package org.dreamwork.tools.network.bridge.client.fx.controllers;

import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ResourceBundle;

public class AboutController implements Initializable {
    public Text content;
    public GridPane root;
    public Hyperlink sourceLink;
    public Hyperlink mail;

    private Stage stage;

    public Stage getStage () {
        return stage;
    }

    public void setStage (Stage stage) {
        this.stage = stage;
        if (stage != null) {
            stage.focusedProperty ().addListener ((b, o, n) -> {
                if (n != null && !n) {
                    stage.close ();
                }
            });
        }
    }

    @Override
    public void initialize (URL location, ResourceBundle resources) {
        content.autosize ();

        root.setOnMouseClicked (e->{
            if (stage != null) {
                stage.close ();
            }
        });

        sourceLink.setOnMouseClicked (e->{
            try {
                java.awt.Desktop.getDesktop ().browse (
                        URI.create ("https://gitee.com/seth_yang/dreamwork-nbc-gui.git")
                );
            } catch (IOException ex) {
                ex.printStackTrace ();
            }
        });

        mail.setOnMouseClicked (e->{
            try {
                java.awt.Desktop.getDesktop ().browse (
                        URI.create ("mailto:seth_yang@126.com")
                );
            } catch (IOException ex) {
                ex.printStackTrace ();
            }
        });
    }

    public void hyperlink (ActionEvent e) {
        Hyperlink link = (Hyperlink) e.getSource ();
        try {
            try {
                java.awt.Desktop.getDesktop ().browse (URI.create (link.getText ()));
            } catch (IOException ex) {
                ex.printStackTrace ();
            }
        } catch (Exception ex) {
            ex.printStackTrace ();
        }
    }
}